package OOM;

public class DeadlockExample {
    private static final Object lock1 = new Object();
    private static final Object lock2 = new Object();

    public static void main(String[] args) {
        Thread thread1 = new Thread(new Runnable() {
            @Override
            public void run() {
                synchronized (lock1) {
                    System.out.println("Thread 1: 持有 lock1");
                    try {
                        Thread.sleep(100); // 等待一段时间，确保线程2能够获取lock2
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    System.out.println("Thread 1: 尝试获取 lock2");
                    synchronized (lock2) {
                        System.out.println("Thread 1: 获取 lock2");
                    }
                }
            }
        });

        Thread thread2 = new Thread(new Runnable() {
            @Override
            public void run() {
                synchronized (lock2) {
                    System.out.println("Thread 2: 持有 lock2");
                    try {
                        Thread.sleep(100); // 等待一段时间，确保线程1能够获取lock1
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    System.out.println("Thread 2: 尝试获取 lock1");
                    synchronized (lock1) {
                        System.out.println("Thread 2: 获取 lock1");
                    }
                }
            }
        });

        thread1.start();
        thread2.start();
    }
}
